angular.module('swan.ui.dialog', ['ui.bootstrap', 'pascalprecht.translate', 'pathModule'])
	//dialog服务，可以用于在js中直接打开dialog，config为配置对象
	.factory("dialog", ['$uibModal', 'swanRelativePath', function($uibModal, swanRelativePath) {
		var dialog = {
			openDialog: function(config) {
				var modalInstance = $uibModal.open({
                     templateUrl: config.templateUrl || swanRelativePath + 'dialog/template/' + config.type + '.html',
                     controller: config.controller || config.type + 'CtrlModalInstance', // 初始化modal的scope
                     size: config.size || '', //大小配置
                     windowTemplateUrl: swanRelativePath + "dialog/template/windowTemplate.html",
                     resolve: {
                         data: config
                     }
                 })
			}
		}
		return dialog;
	}])
    //notify modal controller，cancel函数：点击按钮关闭modal
    .controller('notifyCtrlModalInstance', function($scope, $uibModalInstance, data) { //依赖于modalInstance
        $scope.modalConfig = data;
        $scope.editItem = data.editItem? angular.extend({}, data.editItem) : {};
        $scope.cancel = function() {
            $uibModalInstance.dismiss('cancel'); // 退出
        }
        $scope.ok = function() {
            if (typeof $scope.modalConfig.ok == 'function') {
            	$scope.modalConfig.ok($scope.editItem);
				$uibModalInstance.close("notify"); //操作成功后关闭模态框
            }
            $uibModalInstance.close("notify"); //关闭并返回当前选项
        };
    })
    //confirm modal controller：ok函数：点击按钮关闭modal，提交请求；cancel函数，点击按钮关闭modal，取消请求
    .controller('confirmCtrlModalInstance', function($scope, $uibModalInstance, data) { //依赖于modalInstance
        $scope.modalConfig = data;
        $scope.editItem = data.editItem? angular.extend({}, data.editItem) : {};
        $scope.ok = function() {
            if (typeof $scope.modalConfig.ok == 'function') {
            	$scope.modalConfig.ok($scope.editItem);
				$uibModalInstance.close("confirm"); //操作成功后关闭模态框
            }
            $uibModalInstance.close("confirm"); //关闭并返回当前选项
        };
        $scope.cancel = function() {
            $uibModalInstance.dismiss('cancel'); // 退出
        }
    })
    //edit modal controller：ok函数：点击按钮关闭modal，提交请求（修改或新增内容）；cancel函数，点击按钮关闭modal，取消请求
    .controller('editCtrlModalInstance', function($scope, $uibModalInstance, data) { //依赖于modalInstance
        $scope.modalConfig = data;
        $scope.editItem = data.editItem? angular.extend({}, data.editItem) : {};
        $scope.ok = function() {
            if (typeof $scope.modalConfig.ok == 'function') {
            	$scope.modalConfig.ok($scope.editItem);
            	$uibModalInstance.close("edit"); //操作成功后关闭模态框
            }
        };
        $scope.cancel = function() {
            $uibModalInstance.dismiss('cancel'); //退出
        }
        
        $scope.reset = function() {
        	$scope.user.account = "";
        }
    })
    //显示列表框
    .controller('showCtrlModalInstance', function($scope, $uibModalInstance, data) { //依赖于modalInstance
        $scope.modalConfig = data;
        $scope.editItem = data.editItem? angular.extend({}, data.editItem) : {};
        $scope.cancel = function() {
            $uibModalInstance.dismiss('cancel'); // 退出
        }
    })
    .controller('progressCtrlModalInstance', function($scope, $uibModalInstance, data, $rootScope, $timeout) { //依赖于modalInstance
        $scope.modalConfig = data;
        $scope.modalConfig.errorMsg = '';

        $scope.$watch('modalConfig.progress', function(newValue, oldValue) {
        	if(newValue == 100) {
        		//$scope.modalConfig.msg = "complete";
        		$timeout(function() {
             		$uibModalInstance.close();
                     $scope.$destroy();
             	}, 2000);
        	}
        })
        var _progress = 0;
        var count = 0;
        //间隔time秒刷新一次进度
        var _fakeWaitProgress = function(data) {
            $timeout(function() {
                if (_progress < 100) {
                	 if (count == 9 && _progress == data) {
                		 $scope.modalConfig.errorMsg = 'Upload Error!Timeout!'
                     	$timeout(function() {
                     		$uibModalInstance.close();
                            $scope.$destroy();
                     	}, 2000);
                     	return;
                     }
                	_progress = data;
                    _fakeWaitProgress($scope.modalConfig.progress);
                    count ++;
                }
            }, 1000);
        };
        _fakeWaitProgress($scope.modalConfig.progress);
    })
    .directive('dialogDrt', ['$uibModal', 'swanRelativePath', function($uibModal, swanRelativePath) {
        return {
            restrict: "AC",
            scope: true,
            link: function(scope, iElement, iAttrs) {
            	scope.openDialog = function(obj) { //打开模态 
            		if( typeof(obj) != 'undefined') {
            			scope.messageConfig.editItem = obj;
            		}
                     var modalInstance = $uibModal.open({
                         templateUrl: iAttrs.templateUrl || swanRelativePath + 'dialog/template/' + scope.messageConfig.type + '.html',
                         controller: scope.messageConfig.type + 'CtrlModalInstance', // 初始化modal的scope
                         size: scope.messageConfig.size || '', //大小配置
                         resolve: {
                             data: scope.messageConfig
                         }
                     });
                 }
              
            }
        }
    }]);